//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using EntLibContrib.Resource;
using EntLibContrib.Resource.Providers;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Resource.Configuration.Design.Tests
{
	[TestClass]
	public class CustomResourceProviderNodeFixture : ConfigurationDesignHost
	{
		private static ResourceManager internalResources = InternalResourceFactory.GetResourceManager();

		protected override void InitializeCore ()
		{
			ServiceHelper.GetUIHierarchyService(ServiceProvider).SelectedHierarchy = Hierarchy;

			ResourceConfigurationDesignManager manager = new ResourceConfigurationDesignManager();
			manager.Register(ServiceProvider);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void PassingNullDataInResourceProviderNodeThrowsException ()
		{
			new CustomResourceProviderNode(null);
		}

		[TestMethod]
		public void CanCreateResourceProviderNodeByDataType ()
		{
			ConfigurationNode node = ServiceHelper.GetNodeCreationService(ServiceProvider).CreateNodeByDataType(typeof(CustomResourceProviderData), new object[] { new CustomResourceProviderData() });
			Assert.IsNotNull(node);
			Assert.AreEqual(typeof(CustomResourceProviderNode), node.GetType());
		}

		[TestMethod]
		public void CustomResourceProviderNodeTest ()
		{
			string name = "testName1";
			string resourceBaseName = "testBaseName1";
			Type type = typeof(CustomResourceProviderData);
			NameValueCollection attributes = new NameValueCollection();
			attributes.Add("test", "value");

			CustomResourceProviderNode node = new CustomResourceProviderNode();
			ApplicationNode.AddNode(node);
			Assert.AreEqual("Custom Resource Provider", node.Name);

			node.Name = name;
			node.Type = type.AssemblyQualifiedName;
			node.ResourceBaseName = resourceBaseName;
			node.Attributes.Add(new EditableKeyValue(attributes.GetKey(0), attributes[attributes.GetKey(0)]));

			Assert.AreEqual(attributes[0], node.Attributes[0].Value);
			Assert.AreEqual(name, node.Name);
			Assert.AreEqual(type.AssemblyQualifiedName, node.Type);

			CustomResourceProviderData nodeData = (CustomResourceProviderData)node.ResourceProviderData;
			Assert.AreEqual(name, nodeData.Name);
			Assert.AreEqual(type, nodeData.Type);
			Assert.AreEqual(ResourceProviderData.ResourceBaseNameProperty, nodeData.Attributes.AllKeys[0]);
			Assert.AreEqual(resourceBaseName, nodeData.Attributes.Get(0));
			Assert.AreEqual(attributes.AllKeys[0], nodeData.Attributes.AllKeys[1]);
			Assert.AreEqual(attributes.Get(0), nodeData.Attributes.Get(1));
		}

		[TestMethod]
		public void CustomResourceProviderDataTest ()
		{
			string name = "testName2";
			string resourceBaseName = "testBaseName2";
			Type type = typeof(CustomResourceProviderNodeFixture);
			NameValueCollection attributes = new NameValueCollection();
			attributes.Add("test", "value");

			CustomResourceProviderData data = new CustomResourceProviderData();
			data.Name = name;
			data.Type = type;
			data.ResourceBaseName = resourceBaseName;
			data.Attributes.Add(attributes.GetKey(0), attributes[attributes.GetKey(0)]);

			CustomResourceProviderNode node = new CustomResourceProviderNode(data);
			ApplicationNode.AddNode(node);
			Assert.AreEqual(name, node.Name);
			Assert.AreEqual(type.AssemblyQualifiedName, node.Type);
			Assert.AreEqual(resourceBaseName, node.ResourceBaseName);

			Assert.AreEqual(1, node.Attributes.Count);
			Assert.AreEqual(attributes.AllKeys[0], node.Attributes[0].Key);
			Assert.AreEqual(attributes.Get(0), node.Attributes[0].Value);
		}

		[TestMethod]
		public void EnsureCustomResourceProviderNodePropertyCategoriesAndDescriptions ()
		{
			Assert.IsTrue(ResourceAttributesHelper.AssertResourceDescription(typeof(CustomResourceProviderNode), "Attributes", internalResources["CustomResourceProviderExtensionsDescription"]));
			Assert.IsTrue(ResourceAttributesHelper.AssertResourceCategory(typeof(CustomResourceProviderNode), "Attributes", internalResources["CategoryProvider"]));

			Assert.IsTrue(ResourceAttributesHelper.AssertResourceDescription(typeof(CustomResourceProviderNode), "Type", internalResources["CustomResourceProviderNodeTypeDescription"]));
			Assert.IsTrue(ResourceAttributesHelper.AssertResourceCategory(typeof(CustomResourceProviderNode), "Type", internalResources["CategoryProvider"]));

			Assert.IsTrue(ResourceAttributesHelper.AssertResourceDescription(typeof(CustomResourceProviderNode), "ResourceBaseName", internalResources["ResourceBaseNameDescription"]));
			Assert.IsTrue(ResourceAttributesHelper.AssertResourceCategory(typeof(CustomResourceProviderNode), "ResourceBaseName", internalResources["CategoryProvider"]));
		}
	}
}